package testes.vSys.dao;
import testes.vSys.database.*;

public class DAOFactory {
 
	private static final String packageName = DAOFactory.class.getPackage().getName();
	
	private static IVehicleDao vehicleDao;
	private static IMovimentDao movimentDao;
	private static IModelDao modelDao;
	 
	public static IVehicleDao getVehicleDao() {
		if (vehicleDao == null){
			vehicleDao = (IVehicleDao) newDAOClass("VehicleDao");
		}
		return vehicleDao;
	}

	public static IMovimentDao getMovimentDao() {
		if (movimentDao == null){
			movimentDao = (IMovimentDao) newDAOClass("MovimentDao");
		}
		return movimentDao;
	}
	 
	public static IModelDao getModelDao() {
		if (modelDao == null){
			modelDao = (IModelDao) newDAOClass("ModelDao");
		}
		return modelDao;
	}

	private static Object newDAOClass(String className) {
		Class daoClass = null;
		try {
			daoClass = Class.forName(packageName + "." + DatabaseController.getDatabaseDAOPrefix() + className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		Object instance = null;
		try {
			instance = daoClass.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return instance;
	}
	
}
 
